package org.jianfa.learn.springmvc.controller;

import org.jianfa.learn.springmvc.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@RequestMapping("/ma")
public class MethodArgumentController {


    /**
     * url: http://localhost:8080/demo/handle01
     *
     * String name RequestParamMethodArgument
     *
     * 如果添加@ModelAttribute ServletModelAttributeMethodProcessor
     *
     * ModelAndViewMethodReturnValueHandler
     *
     */
    @RequestMapping("/handle01")
    public ModelAndView handle01(String name) {

        Date date = new Date();// 服务器时间
        // 返回服务器时间到前端页面
        // 封装了数据和页面信息的 ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        // addObject 其实是向请求域中request.setAttribute("date",date);
        modelAndView.addObject("date",date);
        // 视图信息(封装跳转的页面信息) 逻辑视图名
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * HandlerMethodArgumentResolver MapMethodProcessor
     *
     * HandlerMethodReturnValueHandler ViewNameMethodReturnValueHandler
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/handle11")
    public String handle11(ModelMap modelMap) {
        Date date = new Date();// 服务器时间
        modelMap.addAttribute("date",date);
        System.out.println("=================modelmap:" + modelMap.getClass());
        return "success";
    }

    @RequestMapping("/handle04")
    public ModelAndView handle04(User user) {

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date",date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/handle07")
    @ResponseBody
    public User handle07(User user) {

        // 业务逻辑处理，修改name为张三丰
        user.setFirstName("sanfeng");
        user.setLastName("zhang");
        return user;
    }
}
